﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace Campus
{
    static class Utilities
    {
        /// <summary>
        /// This function takes in a string and word wraps it to lineLength size, inserting tabs for each new line if tabNewLines.
        /// 
        /// This function will not wrap mid-word. If there is a word in the string which is longer than the lineLength, then
        /// that word will be added regardless of maximum size.
        /// </summary>
        /// <param name="str">The string to wrap.</param>
        /// <param name="lineLength">The maximum length of each line.</param>
        /// <param name="tabNewLines">Whether or not to insert tabs into each new line.</param>
        /// <returns></returns>
        static public string WordWrap(string str, int lineLength, bool tabNewLines)
        {
            string newString = "";

            string[] lines = str.Split('\n');

            for (int line = 0; line < lines.Length; line++)
            {
                string l = lines[line];

                string newLine = "";

                // Split the string up by spaces and then add the first word to start us off, thus start counting at word 1.
                string[] words = l.Split(' ');
                string currentLine = words[0];
                int i = 1;

                while (i <= words.Length)
                {
                    while (currentLine.Length < lineLength)
                    {
                        // If we're outisde the range, break.
                        if (i >= words.Length)
                        {
                            i++;
                            break;
                        }
                        // If adding the word will be too long, break;
                        if (currentLine.Length + words[i].Length > lineLength && !(words[i].Length > lineLength))
                            break;

                        currentLine += " " + words[i];
                        i++;
                    }

                    // Add our current line to our word wrapped line and clear it.
                    newLine += currentLine;
                    currentLine = "";
                    // Add 5 spaces (tab) if the caller said to.
                    if (tabNewLines)
                        currentLine += "     ";
                    // If we aren't done, then wrap the line.
                    if (i < words.Length)
                        newLine += "\n";
                }

                // Add the word-wrapped version of that line to the string.
                newString += newLine;
                // If this wasn't the last line, add its \n back in.
                if (line < lines.Length - 1)
                    newString += "\n";
            }

            return newString;
        }

        /// <summary>
        /// Returns a floating-point Z-value between 0 and 1 where 0 is in "front" and 1 is in "back" by
        /// </summary>
        /// <param name="s"></param>
        /// <param name="screenHeight"></param>
        /// <param name="playerScreenY"></param>
        /// <returns></returns>
        static public float GetZByScreenPosition(Vector2 position, float drawingAreaHeight)
        {
            float maxZ = 1.0f;

            float z = maxZ - (position.Y / drawingAreaHeight);

            float xValue = position.X + 2.0f;

            z += ((1.0f / xValue)) * 0.01f;

            return z;
        }
    }
}
